package assets_withdraw

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/emun"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type WithdrawPayListPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// c端财务管理页面--提现出款查询
func NewWithdrawPayListPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WithdrawPayListPageLogic {
	return &WithdrawPayListPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WithdrawPayListPageLogic) WithdrawPayListPage(req *types.WithdrawPayListPageReq) (resp *types.WithdrawPayListPageRes, err error) {
	rpc_req := &rpc.WithdrawPayListPageReq{
		UserId:    req.UserId,
		Type:      req.Type,
		OrderNo:   req.OrderNo,
		MixAmount: req.MinAmount,
		MaxAmount: req.MaxAmount,
		Page: &rpc.Page{
			Page:      req.Page.Page,
			PageSize:  req.Page.PageSize,
			SortOrder: req.Page.SortOrder,
		},
	}

	// 发送请求
	rpc_resp, err := l.svcCtx.AssetsRpc.PayWithdraw.WithdrawPayListPage(l.ctx, rpc_req)
	if err != nil {
		return nil, err
	}

	// 处理请求返回的结果
	// 解析、组合数据
	resp = &types.WithdrawPayListPageRes{}
	if rpc_resp.Data != nil {
		resp.Total = rpc_resp.Page.Total
		for i, _ := range rpc_resp.Data {
			d := types.WithDraw{}
			copier.Copy(&d, rpc_resp.Data[i])

			for i2, _ := range rpc_resp.Data[i].Channel {
				d.Channel = append(d.Channel, &types.WithDrawChannel{
					Id:          rpc_resp.Data[i].Channel[i2].ID,
					MappingCode: rpc_resp.Data[i].Channel[i2].MappingCode,
					Name:        rpc_resp.Data[i].Channel[i2].Name,
					MinAmount:   rpc_resp.Data[i].Channel[i2].MinAmount,
					MaxAmount:   rpc_resp.Data[i].Channel[i2].MaxAmount,
					MerchantId:  rpc_resp.Data[i].Channel[i2].MerchantId,
				})
			}
			if rpc_resp.Data[i].UserBank != nil {
				d.UserBank = &types.UserBankInfo{}
				copier.Copy(d.UserBank, rpc_resp.Data[i].UserBank)
			}
			if rpc_resp.Data[i].UserEwallets != nil {
				copier.Copy(d.UserEwallets, rpc_resp.Data[i].UserEwallets)
			}
			if rpc_resp.Data[i].UserLocalEwallets != nil {
				d.UserLocalEwallets = &types.UserLocalEwalletInfo{}
				copier.Copy(d.UserLocalEwallets, rpc_resp.Data[i].UserLocalEwallets)
			}
			if rpc_resp.Data[i].UserPixWallets != nil {
				d.UserPixWallets = &types.UserPixWalletsInfo{
					WalletTypeName: (emun.GetUserPixWalletNameByType(int(rpc_resp.Data[i].UserPixWallets.WalletType))),
				}
				copier.Copy(d.UserPixWallets, rpc_resp.Data[i].UserPixWallets)
			}
			if rpc_resp.Data[i].UserUsdtWallets != nil {
				d.UserUsdtWallets = &types.UserUsdtWalletsInfo{}
				copier.Copy(d.UserUsdtWallets, rpc_resp.Data[i].UserUsdtWallets)
			}
			resp.Data = append(resp.Data, d)
		}

	}
	return
}
